package com.opencloud.ywt.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.opencloud.common.model.PageParams;
import com.opencloud.ywt.client.model.entity.*;
import com.opencloud.ywt.provider.mapper.*;
import com.opencloud.ywt.provider.service.BhyfAngleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 动量服务
 * @author yeyong
 */

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class BhyfAngleServiceImpl implements BhyfAngleService {
    @Autowired
    private BhyfXhbAngleRecordMapper bhyfXhbAngleRecordMapper;
    @Autowired
    private BhyfXhbAngleRecordServicemakeMapper bhyfXhbAngleRecordServicemakeMapper;
    @Autowired
    private BhyfXhbAnglePersonMapper bhyfXhbAnglePersonMapper;
    @Autowired
    private BhyfXhbAngleIntersectionMapper bhyfXhbAngleIntersectionMapper;
    @Autowired
    private BhyfXhbAngleIntersectionClassMapper bhyfXhbAngleIntersectionClassMapper;
    @Autowired
    private BhyfXhbAngleClassMapper bhyfXhbAngleClassMapper;
    @Autowired
    private BhyfXhbAngleObservRecordMapper bhyfXhbAngleObservRecordMapper;
    @Autowired
    private BhyfXhbGoodsMapper bhyfXhbGoodsMapper;
    @Autowired
    private BhyfXhbAngleGoodsMapper bhyfXhbAngleGoodsMapper;
    @Autowired
    private BhyfXhbAngleMachineMapper bhyfXhbAngleMachineMapper;
    @Autowired
    private BhyfXhbAngleGoodsOrderMapper bhyfXhbAngleGoodsOrderMapper;
    @Autowired
    private BhyfXhbAngleGoodsRecordMapper bhyfXhbAngleGoodsRecordMapper;

    @Override
    public IPage<BhyfXhbAngleIntersection> findXhbAngleIntersectionList(int personid,int angleid,int count,String startTime,String endTime) {
        QueryWrapper<BhyfXhbAngleIntersection> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(personid), BhyfXhbAngleIntersection::getPersonid,personid);
        queryWrapper.lambda().eq(angleid>0, BhyfXhbAngleIntersection::getAngleid,angleid);
        queryWrapper.lambda().between(ObjectUtils.isNotEmpty(startTime) && ObjectUtils.isNotEmpty(endTime),BhyfXhbAngleIntersection::getCheckdate, startTime, endTime);
        queryWrapper.lambda().orderByDesc(BhyfXhbAngleIntersection::getSeconds);
        return bhyfXhbAngleIntersectionMapper.selectPage(new Page(1, count), queryWrapper);
    }

    @Override
    public IPage<BhyfXhbAngleIntersectionClass> findXhbAngleIntersectionClassList(int classid,int angleid, int count,String startTime,String endTime) {
        QueryWrapper<BhyfXhbAngleIntersectionClass> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(classid), BhyfXhbAngleIntersectionClass::getClassid,classid);
        queryWrapper.lambda().eq(angleid>0, BhyfXhbAngleIntersectionClass::getAngleid,angleid);
        queryWrapper.lambda().between(ObjectUtils.isNotEmpty(startTime) && ObjectUtils.isNotEmpty(endTime),BhyfXhbAngleIntersectionClass::getCheckdate, startTime, endTime);
        queryWrapper.lambda().orderByDesc(BhyfXhbAngleIntersectionClass::getSeconds);
        return bhyfXhbAngleIntersectionClassMapper.selectPage(new Page(1, count), queryWrapper);
    }


    @Override
    public List<BhyfXhbAngleClass> findXhbAngleClassList(int classid,int angleid,String startTime,String endTime) {
        QueryWrapper<BhyfXhbAngleClass> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(classid), BhyfXhbAngleClass::getClassid,classid);
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(classid), BhyfXhbAngleClass::getAngleclassid,classid);
        queryWrapper.lambda().eq(angleid>0, BhyfXhbAngleClass::getAngleid,angleid);
        queryWrapper.lambda().between(ObjectUtils.isNotEmpty(startTime) && ObjectUtils.isNotEmpty(endTime),BhyfXhbAngleClass::getCheckdate, startTime, endTime);
        queryWrapper.lambda().orderByDesc(BhyfXhbAngleClass::getCheckdate);
        return bhyfXhbAngleClassMapper.selectList(queryWrapper);
    }

    @Override
    public List<BhyfXhbAnglePerson> findXhbAnglePersonList(int classid,int personid,int angleid,String startTime,String endTime) {
        QueryWrapper<BhyfXhbAnglePerson> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(classid>0, BhyfXhbAnglePerson::getClassid,classid);
        queryWrapper.lambda().eq(personid>0, BhyfXhbAnglePerson::getPersonid,personid);
        queryWrapper.lambda().eq(angleid>0, BhyfXhbAnglePerson::getAngleid,angleid);
        queryWrapper.lambda().between(ObjectUtils.isNotEmpty(startTime) && ObjectUtils.isNotEmpty(endTime),BhyfXhbAnglePerson::getCheckdate, startTime, endTime);
        queryWrapper.lambda().orderByDesc(BhyfXhbAnglePerson::getCheckdate);
        return bhyfXhbAnglePersonMapper.selectList(queryWrapper);
    }

    @Override
    public List<BhyfXhbAngleRecord> findXhbAngleRecordList(int classid,int personid,int angleid,String startTime,String endTime) {
        QueryWrapper<BhyfXhbAngleRecord> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(classid>0, BhyfXhbAngleRecord::getClassid,classid);
        queryWrapper.lambda().eq(personid>0, BhyfXhbAngleRecord::getPersonid,personid);
        queryWrapper.lambda().eq(angleid>0, BhyfXhbAngleRecord::getAngleid,angleid);
        queryWrapper.lambda().between(ObjectUtils.isNotEmpty(startTime) && ObjectUtils.isNotEmpty(endTime),BhyfXhbAngleRecord::getCheckdate, startTime, endTime);
        queryWrapper.lambda().orderByDesc(BhyfXhbAngleRecord::getCheckdate);
        return bhyfXhbAngleRecordMapper.selectList(queryWrapper);
    }


    @Override
    public IPage<BhyfXhbAngleObservRecord> findXhbAngleObservRecordList(PageParams pageParams,String startTime,String endTime) {
        QueryWrapper<BhyfXhbAngleObservRecord> queryWrapper = new QueryWrapper();
        BhyfXhbAngleObservRecord query = pageParams.mapToObject(BhyfXhbAngleObservRecord.class);
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getGid()), BhyfXhbAngleObservRecord::getGid,query.getGid());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getClassid()), BhyfXhbAngleObservRecord::getClassid,query.getClassid());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getGradenum()), BhyfXhbAngleObservRecord::getGradenum,query.getGradenum());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getPersonid()), BhyfXhbAngleObservRecord::getPersonid,query.getPersonid());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getRecorduid()), BhyfXhbAngleObservRecord::getRecorduid,query.getRecorduid());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getAngleid()), BhyfXhbAngleObservRecord::getAngleid,query.getAngleid());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getAngletype()), BhyfXhbAngleObservRecord::getAngletype,query.getAngletype());
        queryWrapper.lambda().like(ObjectUtils.isNotEmpty(query.getObservname()), BhyfXhbAngleObservRecord::getObservname,query.getObservname());
        if(ObjectUtils.isNotEmpty(query.getStatus())) {
            queryWrapper.lambda().eq(query.getStatus() > 0, BhyfXhbAngleObservRecord::getStatus, query.getStatus());
        }
        queryWrapper.lambda().ge(BhyfXhbAngleObservRecord::getStatus,1);
        queryWrapper.lambda().between(ObjectUtils.isNotEmpty(startTime) && ObjectUtils.isNotEmpty(endTime),BhyfXhbAngleObservRecord::getCreatetime, startTime, endTime);
        queryWrapper.lambda().orderByDesc(BhyfXhbAngleObservRecord::getCreatetime);
        return bhyfXhbAngleObservRecordMapper.selectPage(new Page(pageParams.getPage(),pageParams.getLimit()), queryWrapper);
    }

    @Override
    public BhyfXhbAngleObservRecord findXhbAngleObservRecordById(Integer id)
    {
        return bhyfXhbAngleObservRecordMapper.selectById(id);
    }


    @Override
    public BhyfXhbAngleObservRecord addBhyfXhbAngleObservRecord(BhyfXhbAngleObservRecord entiy)
    {

        bhyfXhbAngleObservRecordMapper.insert(entiy);
        return entiy;
    }

    @Override
    public void updateBhyfXhbAngleObservRecord(BhyfXhbAngleObservRecord entiy)
    {
        bhyfXhbAngleObservRecordMapper.updateById(entiy);
    }


    @Override
    public BhyfXhbAngleObservRecord findRecordInfoById(int id)
    {
        return bhyfXhbAngleObservRecordMapper.selectById(id);
    }

    @Override
    public List<BhyfXhbAngleRecordTongJiResult> getAngleRecordTongJi(int gid,int classid,String checkdate)
    {
        return bhyfXhbAngleRecordMapper.getAngleRecordTongJi(gid,classid,checkdate);
    }

    @Override
    public List<BhyfXhbAngleRecordTongJiPerson> getAngleRecordPersonTongJi(int gid,int classid,String checkdate)
    {
        return bhyfXhbAngleRecordMapper.getAngleRecordPersonTongJi(gid,classid,checkdate);
    }

    @Override
    public BhyfXhbAngleGoods addBhyfXhbAngleGoods(BhyfXhbAngleGoods entiy)
    {
        bhyfXhbAngleGoodsMapper.insert(entiy);
        return entiy;
    }

    @Override
    public void updateBhyfXhbAngleGoods(BhyfXhbAngleGoods entiy)
    {
        bhyfXhbAngleGoodsMapper.updateById(entiy);
    }

    @Override
    public BhyfXhbGoods addBhyfXhbGoods(BhyfXhbGoods entiy)
    {
        bhyfXhbGoodsMapper.insert(entiy);
        return entiy;
    }


    @Override
    public BhyfXhbAngleGoodsRecord addBhyfXhbAngleGoodsRecord(BhyfXhbAngleGoodsRecord entiy)
    {
        bhyfXhbAngleGoodsRecordMapper.insert(entiy);
        return entiy;
    }

    @Override
    public void updateBhyfXhbGoods(BhyfXhbGoods entiy)
    {
        bhyfXhbGoodsMapper.updateById(entiy);
    }


    @Override
    public void deleteBhyfXhbGoods(Integer id)
    {

        bhyfXhbGoodsMapper.deleteById(id);
    }

    @Override
    public BhyfXhbGoods findBhyfXhbGoodsById(Integer id)
    {

        return bhyfXhbGoodsMapper.selectById(id);
    }


    @Override
    public BhyfXhbAngleGoods findBhyfXhbAngleGoodsById(Integer id)
    {

        return bhyfXhbAngleGoodsMapper.selectById(id);
    }

    @Override
    public IPage<BhyfXhbGoods> findBhyfXhbGoodsListPage(PageParams pageParams) {
        BhyfXhbGoods query = pageParams.mapToObject(BhyfXhbGoods.class);
        QueryWrapper<BhyfXhbGoods> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getGid()), BhyfXhbGoods::getGid,query.getGid());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getClassid()), BhyfXhbGoods::getClassid,query.getClassid());
        queryWrapper.lambda().orderByDesc(BhyfXhbGoods::getCreatetime);
        return bhyfXhbGoodsMapper.selectPage(new Page(pageParams.getPage(), pageParams.getLimit()), queryWrapper);
    }

    @Override
    public IPage<BhyfXhbAngleGoods> findBhyfXhbAngleGoodsListPage(PageParams pageParams) {
        BhyfXhbAngleGoods query = pageParams.mapToObject(BhyfXhbAngleGoods.class);
        QueryWrapper<BhyfXhbAngleGoods> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(BhyfXhbAngleGoods::getStatus,1);
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getGid()), BhyfXhbAngleGoods::getGid,query.getGid());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getClassid()), BhyfXhbAngleGoods::getClassid,query.getClassid());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getAngleid()), BhyfXhbAngleGoods::getAngleid,query.getAngleid());
        queryWrapper.lambda().orderByDesc(BhyfXhbAngleGoods::getCreatetime);
        return bhyfXhbAngleGoodsMapper.selectPage(new Page(pageParams.getPage(), pageParams.getLimit()), queryWrapper);
    }


    @Override
    public List<BhyfXhbAngleMachine> getAngleMachineList(int gid,int classid,int angleid)
    {
        QueryWrapper<BhyfXhbAngleMachine> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(gid), BhyfXhbAngleMachine::getGid,gid);
        queryWrapper.lambda().eq(classid>0, BhyfXhbAngleMachine::getClassid,classid);
        queryWrapper.lambda().eq(angleid>0, BhyfXhbAngleMachine::getAngleid,angleid);
        return bhyfXhbAngleMachineMapper.selectList(queryWrapper);
    }


    @Override
    public List<BhyfXhbAngleGoodsOrder> getXhbAngleGoodsOrderList(int gid,int classid,int angleid,int personid,String startTime,String endTime)
    {
        QueryWrapper<BhyfXhbAngleGoodsOrder> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(gid), BhyfXhbAngleGoodsOrder::getGid,gid);
        queryWrapper.lambda().eq(classid>0, BhyfXhbAngleGoodsOrder::getClassid,classid);
        queryWrapper.lambda().eq(angleid>0, BhyfXhbAngleGoodsOrder::getAngleid,angleid);
        queryWrapper.lambda().eq(personid>0, BhyfXhbAngleGoodsOrder::getPersonid,personid);
        queryWrapper.lambda().between(ObjectUtils.isNotEmpty(startTime) && ObjectUtils.isNotEmpty(endTime),BhyfXhbAngleGoodsOrder::getOrdertime, startTime, endTime);
        return bhyfXhbAngleGoodsOrderMapper.selectList(queryWrapper);
    }

    @Override
    public void deleteBhyfXhbAngleObservRecord(Integer id)
    {

        bhyfXhbAngleObservRecordMapper.deleteById(id);
    }

    @Override
    public List<BhyfXhbAnglePersonCount> getAnglePersonCountAndSecondsTongji(int gid,int classid,String startTime,String endTime)
    {
        return bhyfXhbAnglePersonMapper.getAnglePersonCountAndSecondsTongji(gid,classid,startTime,endTime);
    }


    @Override
    public List<BhyfXhbAngleGoodsResult> getBhyfXhbAngleGoodsResultList(int gid,int classid,int angleid)
    {
        return bhyfXhbAngleGoodsMapper.getXhbAngleGoodsResult(gid,classid,angleid);
    }

    @Override
    public List<BhyfXhbAngleGoods> IsExistAngleGoods(int angleid,int classid,String goodsno,String mac) {
        QueryWrapper<BhyfXhbAngleGoods> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(BhyfXhbAngleGoods::getStatus,1);
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(angleid), BhyfXhbAngleGoods::getAngleid,angleid);
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(goodsno), BhyfXhbAngleGoods::getGoodsno,goodsno);
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(classid), BhyfXhbAngleGoods::getClassid,classid);
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(mac), BhyfXhbAngleGoods::getMac,mac);
        queryWrapper.lambda().orderByDesc(BhyfXhbAngleGoods::getCreatetime);
        return bhyfXhbAngleGoodsMapper.selectList(queryWrapper);
    }

    @Override
    public String getAngleNames(int gid,int classid,int personid,String checkdate)
    {
        return bhyfXhbAngleRecordMapper.getAngleRecordPersonAngleNames(gid,classid,personid,checkdate);
    }


    @Override
    public List<BhyfXhbAnglePlayResult> getAngleRecordPlayResult(int gid,int classid,String startdate,String enddate)
    {
        return bhyfXhbAngleRecordMapper.getAngleRecordPlayResult(gid,classid,startdate,enddate);
    }


    @Override
    public List<BhyfXhbAnglePlayGoodsResult> getAngleGoodsPlayResult(int gid,int classid,String startdate,String enddate)
    {
        return bhyfXhbAngleRecordMapper.getAngleGoodsPlayResult(gid,classid,startdate,enddate);
    }

    @Override
    public List<BhyfXhbAngleRecordTongJiPerson> getRealtimeAngleRecordPersonTongJi(int gid,int classid,String checkdate)
    {
        return bhyfXhbAngleRecordMapper.getRealtimeAngleRecordPersonTongJi(gid,classid,checkdate);
    }


    @Override
    public List<BhyfXhbAngleRecordServicemake> findRealtimeXhbAngleRecordList(int classid,int personid,int angleid,String startTime,String endTime) {
        QueryWrapper<BhyfXhbAngleRecordServicemake> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(classid>0, BhyfXhbAngleRecordServicemake::getClassid,classid);
        queryWrapper.lambda().eq(personid>0, BhyfXhbAngleRecordServicemake::getPersonid,personid);
        queryWrapper.lambda().eq(angleid>0, BhyfXhbAngleRecordServicemake::getAngleid,angleid);
        queryWrapper.lambda().between(ObjectUtils.isNotEmpty(startTime) && ObjectUtils.isNotEmpty(endTime),BhyfXhbAngleRecordServicemake::getCheckdate, startTime, endTime);
        queryWrapper.lambda().orderByDesc(BhyfXhbAngleRecordServicemake::getCheckdate);
        return bhyfXhbAngleRecordServicemakeMapper.selectList(queryWrapper);
    }

    @Override
    public List<BhyfXhbAnglePerson> GetBhfyXhbAnglePersonListByService(int classid,int personid,int angleid,String startTime,String endTime)
    {
        return bhyfXhbAngleRecordServicemakeMapper.GetBhfyXhbAnglePersonListByService(classid,personid,angleid,startTime,endTime);
    }

    @Override
    public List<BhyfXhbAngleClass> GetBhfyXhbAngleClassListByService(int classid,int angleid,String startTime,String endTime)
    {
        return bhyfXhbAngleRecordServicemakeMapper.GetBhfyXhbAngleClassListByService(classid,angleid,startTime,endTime);
    }

    @Override
    public List<BhyfXhbAngleRecordTongJiPerson> GetAnglePersonTongjiByService(int gid,int classid,String startTime,String endTime)
    {
        return bhyfXhbAngleRecordServicemakeMapper.GetAnglePersonTongjiByService(gid,classid,startTime,endTime);
    }

}